<template>
    <uv-calendars ref="calendars" @change="change" :insert="true" :showMonth="false" :selected="selected" />
    <uv-tabs :itemStyle="{ height: '88rpx' }" :list="tabList" @click="tabListclick" :scrollable="false"
        lineWidth="300rpx" lineColor="#1f87c6" activeStyle="color:#1f87c6;font-weight: bold;"></uv-tabs>
    <view v-if="current == 0">
        <uv-tabs :itemStyle="{ height: '88rpx' }" :list="tabList1" @click="tab1Listclick" :current="current1"
            :scrollable="false" lineWidth="200rpx" lineColor="#1f87c6"
            activeStyle="color:#1f87c6;font-weight: bold;"></uv-tabs>
        <view v-if="current1 == 0">
            <view class="havedata mx-32" v-if="warningcon.length > 0">
                <view v-for="(item, i) in warningcon" :key="i">
                    <view class="doctext">{{ item.warnContent }}</view>
                    <view class="doctext">{{ item.suggestContent }}</view>
                    <view class="waining-img">
                        <image mode="widthFix" :src="item.imgUrl" style="width: 100%;" />
                    </view>
                </view>
                <view class="docbutton mt-40 pb-30" v-if="docUrl">
                    <a :href="docUrl" target="_blank" rel="nofollow">
                        <button type="button">文档下载</button>
                    </a>
                </view>
            </view>
            <view class="nodata" v-else>暂无信息</view>
        </view>
        <view v-if="current1 == 1">
            <view class="havedata mx-32" v-if="centerdata.length > 0">
                <view v-for="(item, i) in centerdata" :key="i">
                    <view class="doctext">{{ item.warnContent }}</view>
                    <view class="doctext">{{ item.suggestContent }}</view>
                    <view class="waining-img">
                        <image mode="widthFix" :src="item.imgUrl" style="width: 100%;" />
                    </view>
                </view>
                <view class="docbutton mt-40 pb-30" v-if="docUrl1">
                    <a :href="docUrl1" target="_blank" rel="nofollow">
                        <button type="button">文档下载</button>
                    </a>
                </view>
            </view>
            <view class="nodata" v-else>暂无信息</view>
        </view>
    </view>
    <view v-if="current == 1">
        <uv-tabs :itemStyle="{ height: '88rpx' }" :list="tabList2" @click="tab2Listclick" :current="current2"
            :scrollable="false" lineWidth="200rpx" lineColor="#1f87c6"
            activeStyle="color:#1f87c6;font-weight: bold;"></uv-tabs>
        <view class="mx-32 py-32">
            <uv-form ref="formRef" :model="formData" :rules="rules" labelWidth="150rpx" labelPosition="top"
                :labelStyle="{ fontSize: '30rpx' }">
                <uv-form-item :required="true" label="文件名称" prop="Name" borderBottom>
                    <uv-input border="none" v-model="formData.Name" placeholder="请输入文件名称"
                        placeholderStyle="font-size:28rpx" fontSize="30rpx"> </uv-input>
                </uv-form-item>
                <uv-form-item :required="true" label="上传文件" prop="file">
                    <view style="width:100%">
                        <uploadFile @update="updateFn" ref="uploadRef"></uploadFile>
                    </view>
                </uv-form-item>
                <uv-button type="primary" @click="submitForm">提交文件</uv-button>
            </uv-form>
        </view>
    </view>
</template>

<script lang="ts" setup>
import uploadFile from './components/upload-file.vue';
import { GetWarnProductNewest_API, getWarnProducts_API } from '@/server/mapdzfz';
import uploadfiles from '@/server/uploadfiles';
import { getLoginMsg } from '@/utils/util';
import dayjs from 'dayjs';


const selected = ref([])
let systemTime = dayjs().format('YYYY-MM-DD')

const change = (e: any) => {
    systemTime = e.fulldate
    getAllList()
}

const getWarnProducts = async () => {
    const res = await getWarnProducts_API({ code: 430000, year: '', type: '', name: '' })
    const regular = /^jsoncallback\({result:'(.*)'}\)$/;
    const zzpp = res.match(regular);
    const data = JSON.parse(zzpp[1])
    selected.value = data.map(item => {
        return {
            date: item.producttime,
            badge: true,
            // info: item.region,
        };
        // switch (item.region) {
        //     case 3:
        //         warningValue.info = ""
        //         break;
        //     case 2:
        //         warningValue.info = ""
        //         break;
        //     case 1:
        //        warningValue.info = ""
        //         break;
        // }
    })
    console.log("selected.value:", selected.value);
}
onLoad(() => {
    getWarnProducts()
    getAllList()
})
const current = ref(0)
const tabList = [
    { name: '省级预警' },
    { name: '县级预警' }
]
const tabListclick = (e: any) => {
    current.value = e.index
}
const current1 = ref(0)
const tabList1 = [
    { name: '短期预警' },
    { name: '中短期预警' }
]
const tab1Listclick = (e: any) => {
    current1.value = e.index
    getAllList()
}
const current2 = ref(0)
const tabList2 = [
    { name: '县级短期预警' },
    { name: '县级气象预警' }
]
const tab2Listclick = (e: any) => {
    current2.value = e.index
}
const warningcon = ref([])
const docUrl = ref('')
const centerdata = ref([])
const docUrl1 = ref('')
const getAllList = () => {
    try {
        uni.showLoading({ title: '加载中' });
        if (current1.value == 0) {
            getList()
            getList1()
        } else {
            getList2()
            getList3()
        }
    } finally {
        uni.hideLoading();
    }

}
//短期预警
const getList = async () => {
    const res = await GetWarnProductNewest_API({
        callback: 'jsoncallback',
        time: systemTime,
        warntype: 2,
        productType: 1,
        code: 430000,
        name: '湖南省',
    })
    let regular = /^jsoncallback\({result:'(.*)'}\)$/;
    let zzpp = res.match(regular);
    let xx = zzpp[1];
    if (xx == 'nodataexist') {
        return warningcon.value = []
    }
    warningcon.value = JSON.parse(xx)
}
//短期预警doc
const getList1 = async () => {
    const res = await GetWarnProductNewest_API({
        callback: 'jsoncallback',
        time: systemTime,
        warntype: 2,
        productType: 0,
        code: 430000,
        name: '湖南省',
    })
    let regular = /^jsoncallback\({result:'(.*)'}\)$/;
    let zzpp = res.match(regular);
    let xx = zzpp[1];
    if (xx == 'nodataexist') {
        return docUrl.value = ''
    }
    docUrl.value = xx
}
//短期预警doc
const getList2 = async () => {
    const res = await GetWarnProductNewest_API({
        callback: 'jsoncallback',
        time: systemTime,
        warntype: 4,
        productType: 1,
        code: 430000,
        name: '湖南省',
    })
    let regular = /^jsoncallback\({result:'(.*)'}\)$/;
    let zzpp = res.match(regular);
    let xx = zzpp[1];
    if (xx == 'nodataexist') {
        return centerdata.value = []
    }
    centerdata.value = JSON.parse(xx)
}
//短期预警doc
const getList3 = async () => {
    const res = await GetWarnProductNewest_API({
        callback: 'jsoncallback',
        time: systemTime,
        warntype: 4,
        productType: 0,
        code: 430000,
        name: '湖南省',
    })
    let regular = /^jsoncallback\({result:'(.*)'}\)$/;
    let zzpp = res.match(regular);
    let xx = zzpp[1];
    if (xx == 'nodataexist') {
        return docUrl1.value = ''

    }
    docUrl1.value = xx
}
const rules = {
    Name: {
        required: true,
        message: '请输入文件名称',
        trigger: ['change', 'blur']
    },
    file: {
        required: true,
        message: '请选择文件',
    },
}
const formData = ref({
    file: '',
    Name: '',
    TypeCode: 'DQYJ1',
    FKey: getLoginMsg()?.AdminCode
})

const updateFn = (e) => {
    formData.value.file = e[0]?.url || ''
}
const formRef = ref(null)
const uploadRef = ref(null)
const submitForm = () => {
    formRef.value.validate().then(async () => {
        const res = await uploadfiles.uploadImg_API({ file: formData.value.file }, {
            TypeCode: current2.value == 0 ? 'DQYJ' : 'QXYJ',
            FKey: formData.value.FKey,
            Name: formData.value.Name,
        })
        if (res.IsSuccess) {
            uni.showToast({
                title: '上传成功',
                icon: 'success'
            })
            formData.value.file = ''
            formData.value.Name = ''
            uploadRef.value.reset()
        } else {
            uni.showToast({
                title: '上传失败',
                icon: 'error'
            })
        }
    })
}

</script>

<style lang="scss" scoped>
.doctext {
    color: #333;
    padding-top: 16rpx;
    font-size: 28rpx;
}

.nodata {
    text-align: center;
    font-size: 32rpx;
    padding-top: 60rpx;
    padding-bottom: 60rpx;
}

.docbutton a {
    text-decoration: none;
}

button {
    height: 88rpx;
    line-height: 88rpx;
    color: #fff;
    font-size: 32rpx;
    border: none;
    border-radius: 8rpx;
    background: #1F87C6;
}
</style>
